package com.tridoron.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tridoron.reggie.common.CustomException;
import com.tridoron.reggie.common.R;
import com.tridoron.reggie.dto.OrdersDto;
import com.tridoron.reggie.dto.SetmealDto;
import com.tridoron.reggie.entity.Category;
import com.tridoron.reggie.entity.Orders;
import com.tridoron.reggie.entity.Setmeal;
import com.tridoron.reggie.service.OrderService;
import com.tridoron.reggie.utils.TimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        orderService.submit(orders);
        return R.success("下单成功");
    }

    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String number,String beginTime,String endTime){
        Page<Orders> pageInfo=new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage=new Page<>();
        Long ordersId;
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        if (number!=null){
            try {
                ordersId=Long.valueOf(number);
                // 添加查询条件：订单号等于指定的 orderNumber
                queryWrapper.eq(Orders::getNumber, ordersId);
            }catch (Exception ex){//NumberFormatException
                throw new CustomException("请输入正确的订单号");
            }
        }
        if (beginTime!=null && endTime!=null){
            LocalDateTime bTime = TimeUtils.parseStringToDateTime(beginTime);
            LocalDateTime eTime = TimeUtils.parseStringToDateTime(endTime);
            // 添加查询条件：订单时间在开始时间和结束时间之间
            queryWrapper.between(Orders::getOrderTime, bTime, eTime);
        }
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
//        List<Orders> records=pageInfo.getRecords();
//        List<OrdersDto> list=records.stream().map((item)->{
//            OrdersDto ordersDto=new OrdersDto();
//            BeanUtils.copyProperties(item,ordersDto);
//
//        }).collect(Collectors.toList());

        return R.success(pageInfo);
    }

    @PutMapping
    public R<String> save(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("订单信息修改成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page,int pageSize){
        //分页构造器对象
        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据更新时间降序排列
        queryWrapper.orderByDesc(Orders::getOrderTime);
        orderService.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item,ordersDto);
//            //分类id
//            Long dishId = item.get();
//            //根据分类id查询分类对象
//            Category category = orderService.getById(categoryId);
//            if(category != null){
//                //分类名称
//                String categoryName = category.getName();
//                setmealDto.setCategoryName(categoryName);
//            }
            return ordersDto;
        }).collect(Collectors.toList());
//
        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }
}